<?php
include('lib/adodb-time.inc.php');
include('lib/ftree_utils.php');
require_once "HTML/Template/IT.php";

function GetSpouseAndKids($myID, $myFull, $mySex, $lvl)
{
	global $rpt, $showDOB, $showSpouses;

	//$query = "SELECT * FROM person WHERE `id` = $myID";
	//$myresult = mysql_query($query) or die("Query failed : " . mysql_error());
	//$myData = mysql_fetch_array($myresult, MYSQL_ASSOC);
	
	//$myFull = formatName($myData['first'], $myData['middle'], $myData['last'], $myData['suffix'], $myData['nick'], "fnmls");
	if ($mySex == "M") {
		$asSpouseFld = "husband";
		$mySpouseFld = "wife";
	} else {
		$asSpouseFld = "wife";
		$mySpouseFld = "husband";
	}
	
	$query = "SELECT $mySpouseFld, date FROM marriage WHERE `" .$asSpouseFld. "` = $myID ORDER BY `date` ASC";
	$marriage_result = mysql_query($query) or die("Query failed : " . mysql_error());

	$rpt .= "<ul>\n";
	
	while ($aMarriage = mysql_fetch_array($marriage_result, MYSQL_ASSOC)) {
		$mySpouseID = $aMarriage["$mySpouseFld"];

		if ($showSpouses) {
			$query = "SELECT * FROM person WHERE `id` = $mySpouseID";
			$mySpouse_result = mysql_query($query) or die("Query failed : " . mysql_error());
			$mySpouseData = mysql_fetch_array($mySpouse_result, MYSQL_ASSOC);
	
			$mySpouseFull = formatName($mySpouseData['first'], $mySpouseData['middle'], $mySpouseData['last'], $mySpouseData['suffix'], $mySpouseData['nick'], "fnmls");
			$myDOM = formatDate($aMarriage['date'], "short");
			if ($myDOM != "") { $myDOM = "(".$myDOM.")"; }
			$rpt .= "<li><img src=\"icons/rings.png\" alt=\"m.\" border=\"0\" width=\"20\" /> $myDOM <a href=\"person.php?id=$mySpouseData[id]\">$mySpouseFull</a>";
			if ($showDOB && $mySpouseData['dob'] != "0000-00-00") { $rpt .= " (".formatDate($mySpouseData[dob], "short").")"; }
			$rpt .= "</li>\n";
		}
		$lvl++;
		GetSpouseKids($myID, $mySpouseID, $mySex, $lvl);
		$lvl--;
	}
	$rpt .= "</ul>\n";
}

function GetSpouseKids($myID, $spouseID, $mySex, $lvl)
{
	global $rpt, $showDOB, $cntDesc, $limitDesc, $maxDesc;

	if ($limitDesc && $lvl > $maxDesc) { return; }
	if ($mySex == "M") {
		$asParentFld  = "father";
		$othParentFld = "mother";
	} else {
		$asParentFld  = "mother";
		$othParentFld = "father";
	}

	$query = "SELECT * FROM person WHERE `".$asParentFld."` = $myID AND `$othParentFld` = $spouseID ORDER BY `dob` ASC";
	$kids_result = mysql_query($query) or die("Query failed : " . mysql_error());

	$rpt .= "<ol>\n";

	while ($aKid = mysql_fetch_array($kids_result, MYSQL_ASSOC)) {
		$aKidID = $aKid['id'];

		$query = "SELECT * FROM person WHERE `id` = $aKidID";
		$aKid_result = mysql_query($query) or die("Query failed : " . mysql_error());
		$aKidData = mysql_fetch_array($aKid_result, MYSQL_ASSOC);

		$aKidFull = formatName($aKidData['first'], $aKidData['middle'], $aKidData['last'], $aKidData['suffix'], $aKidData['nick'], "fnmls");
		$aKidShort = formatName($aKidData['first'], $aKidData['middle'], $aKidData['last'], $aKidData['suffix'], $aKidData['nick'], "fm");
		$rpt .= "<li><a href=\"person.php?id=$aKidData[id]\">$aKidShort</a>";
		if ($showDOB && $aKidData['dob'] != "0000-00-00") {
			$rpt .= " (".formatDate($aKidData['dob'],"short").")";
		}
		$rpt .= "</li>\n";
		$cntDesc[$lvl]++;
		GetSpouseAndKids($aKidData['id'], $aKidFull, $aKidData['sex'], $lvl);
	}
	$rpt .= "</ol>\n";

}


// MAIN

// GET HTTP "GET" VARS

if ( isset($_GET['id']) ) {
        $id = $_GET['id'];
} else {
        $id = 3054;
}
$id=preg_replace("/[^0-9]/","",$id);  //get rid of anything but numbers
if ($id < 1 || $id > pow(2,17)) {
        die("ID is out of range.");
}
if ( isset($_POST['showDOB']) ) {
	$showDOB = TRUE;
} else {
	$showDOB = FALSE;
}
if ( isset($_POST['showSpouses']) ) {
	$showSpouses = TRUE;
} else {
	$showSpouses = FALSE;
}
if ($_POST['maxDesc'] == "All") {
	$limitDesc = FALSE;
	$maxDescLabel="all";
} else {
	$limitDesc = TRUE;
	$maxDesc = $_POST['maxDesc'];
	$maxDescLabel=$maxDesc;
}

// SET UP TEMPLATE

$isMobile = ChkIsMobile();
$tpl = new HTML_Template_IT("templates");
$isMobile ? $template = "report_desc_mtmpl.html" : $template = "report_desc_tmpl.html";
$status = $tpl->loadTemplatefile($template, true, true);

/* Connecting, selecting database */
$link = mysql_connect("localhost", "ftreeweb", "ftree_web")
   or die("Could not connect : " . mysql_error());
mysql_select_db("ftree") or die("Could not select database");


// GET PERSON

$query = "SELECT * FROM person WHERE `id` = $id";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$idData = mysql_fetch_array($result, MYSQL_ASSOC);

$full = formatName($idData['first'], $idData['middle'], $idData['last'], $idData['suffix'], $idData['nick'], "fnmls");

$tpl->setVariable("PersonID", $id);
$tpl->setVariable("FullName", $full);

//$rpt = "<h2>Descendents to $_POST[maxDesc] generations:</h2>\n";
$rpt = "<h2>Descendents to $maxDescLabel generations:</h2>\n";

$cntDesc = array(0,0,0,0);  //up to GGChildren

GetSpouseAndKids($id, $full, $idData['sex'], 0);

$tpl->setVariable("ReportData", $rpt);
$tpl->setVariable("CntChildren", $cntDesc[1]);
$tpl->setVariable("CntGChildren", $cntDesc[2]);
$tpl->setVariable("CntGGChildren", $cntDesc[3]);

mysql_close($link);

$tpl->show();
?>
